<template>
  <div class="coupon_tabs">
    <van-tabs v-model:active="TabActive" type="card" sticky @click-tab="OnTab">
      <van-tab
        v-for="item in TabList"
        :title="item.title"
        :name="item.status"
        :key="item.status"
      ></van-tab>
    </van-tabs>
  </div>

  <!-- 占位 -->
  <div style="height: 60px"></div>

  <div class="coupon_list">
    <van-pull-refresh v-model="refreshing" @refresh="OnRefresh">
      <van-list
        v-model:loading="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="OnLoad"
      >
        <div class="list">
          <div class="coupon_item" v-for="item in list" :key="item.id">
            <div class="item_content">
              <router-link
                :to="{ path: '/coupon/info', query: { cid: item.coupon.id } }"
              >
                <div class="left">
                  {{ item.coupon.rate * 100 }}<span>折</span>
                </div>
                <div class="right">
                  <div>{{ item.coupon.title }}</div>
                  <div>活动开始时间：{{ item.coupon.createtime_date }}</div>
                  <div>活动结束时间：{{ item.coupon.endtime_date }}</div>
                </div>
              </router-link>
            </div>
            <div class="item_btn">
              <van-button
                type="primary"
                class="right_btn"
                v-if="item.status == 1"
                @click="ToProduct"
                >去使用</van-button
              >
              <van-button type="danger" disabled class="right_btn" v-else
                >已使用</van-button
              >
            </div>
          </div>

          <van-back-top :bottom="'80'" />
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script setup>
// this代理
const { proxy } = getCurrentInstance();
// 用户数据
const business = proxy.$business;
// 存放数据
const list = ref([]);
// 刷新状态
const refreshing = ref(false);
// 懒加载状态
const loading = ref(false);
// 上拉加载状态
const finished = ref(false);
// 当前页
const page = ref(1);
// tab选中状态
const TabActive = ref(1);
// tab数据
const TabList = ref([
  {
    title: "待使用",
    status: 1,
  },
  {
    title: "已使用",
    status: 0,
  },
]);

// 挂载前钩子
onBeforeMount(() => OnRefresh());

// 切换Tab栏
const OnTab = debounce(() => {
  OnRefresh();
}, 1000);

// 下拉刷新回调函数
const OnRefresh = () => {
  page.value = 1;
  list.value = [];
  finished.value = false;
  loading.value = true;
  refreshing.value = true;

  setTimeout(OnLoad, 1000);
};

// 上拉加载回调函数
const OnLoad = async () => {
  // 如果是下拉刷新，重置其值
  if (refreshing.value === true) {
    refreshing.value = false;
  }
  // 组装参数
  let params = {
    busid: business.value.id,
    status: TabActive.value,
    page: page.value,
  };
  // 发起请求
  let result = await proxy.$http.post({
    url: "/business/coupon",
    params,
  });
  // 处理请求结果
  if (result.code === 1) {
    // 获取数据总数
    let count = result.data ? result.data.count : 0;
    // 获取数据并且合并数组
    list.value = result.data ? list.value.concat(result.data.list) : [];
    // 判断是否加载完所有数据
    if (list.value.length >= count) {
      // 完成加载
      finished.value = true;
      return false;
    }
    // 分页+1
    page.value = page.value + 1;
  } else {
    finished.value = true;
  }
};

// 去使用，跳转到商品页
const ToProduct = () => {
  proxy.$router.push("/product");
};
</script>

<style scoped>
@import url("/assets/css/CouponIndex.css");
</style>